package com.hyt.it.ogt.kq.main.controller.bm;

import cn.hutool.core.util.StrUtil;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.core.validate.Insert;
import com.hyt.exception.annotation.ThrowsAdvice;
import com.hyt.it.ogt.kq.common.bm.utils.Constant;
import com.hyt.it.ogt.kq.main.controller.base.BaseController;
import com.hyt.it.ogt.kq.service.bm.model.dto.ProjectNodeDTO;
import com.hyt.it.ogt.kq.service.bm.model.dto.ProjectNodeListDTO;
import com.hyt.it.ogt.kq.service.bm.model.entity.Project;
import com.hyt.it.ogt.kq.service.bm.model.entity.ProjectNode;
import com.hyt.it.ogt.kq.service.bm.model.param.ProjectNodeBaseProjectParam;
import com.hyt.it.ogt.kq.service.bm.model.param.ProjectNodeCollectInfoParam;
import com.hyt.it.ogt.kq.service.bm.model.param.ProjectNodeParam;
import com.hyt.it.ogt.kq.service.bm.model.param.ProjectNodeSubjectParam;
import com.hyt.it.ogt.kq.service.bm.model.vo.ProjectNodeVo;
import com.hyt.it.ogt.kq.service.bm.service.IProject2Service;
import com.hyt.log.annotation.Log;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @Description:
 * @Author: STARF
 * @CreateDate: 2022/03/03 17:01
 **/
@Slf4j
@RestController
@Api(tags = "24.报名流程", value = "报名流程")
@ApiSort(value = 24)
@RequestMapping("/bm/project-node")
public class ProjectNodeController extends BaseController {

    @Resource
    private IProject2Service iProject2Service;

    @ApiOperation(value = "24.1.1 编辑报名基本信息项目", notes = "20000:成功；31241101：编辑项目基本信息失败")
    @ApiOperationSort(value = 1)
    @Deprecated
    @Log(module = "报名流程管理", business = "编辑报名基本信息项目")
    @ThrowsAdvice(code = 31241101, print = "编辑项目基本信息失败")
    @RequestMapping(value = "/updateProjectBaseInfo", method = RequestMethod.POST)
    public Object updateProjectBaseInfo(@RequestBody Project project) {
        Integer code = iProject2Service.verifyProjectDate(project);
        if (null != code) {
            return ApiResponse.builder().code(code).build();
        }
        Integer code2 = iProject2Service.updateProjectBaseInfo(project);
        if (null == code2) {
            return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).build();
        } else {
            return ApiResponse.builder().code(code2).build();
        }
    }

    @ApiOperation(value = "24.1.2 项目流程保存", notes = "20000:成功；31241201：项目流程保存失败")
    @ApiOperationSort(value = 2)
    @ThrowsAdvice(code = 31241201, print = "项目流程保存异常")
    @Log(module = "报名流程管理", business = "项目流程保存")
    @RequestMapping(value = "/setProjectNode", method = RequestMethod.POST)
    public ApiResponse<Integer> setProjectNode(@RequestBody ProjectNodeListDTO listDTO) {
        return ApiResponse.<Integer>builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .data(iProject2Service.setProjectNode(listDTO))
                .build();
    }

    @ApiOperation(value = "24.1.3 获取报名流程列表", notes = "20000:成功；31241301：获取报名流程列表失败")
    @ApiOperationSort(value = 3)
    @ThrowsAdvice(code = 31241301, print = "获取报名流程列表异常")
    @RequestMapping(value = "/queryProjectNode", method = RequestMethod.POST)
    public ApiResponse<List<ProjectNodeVo>> queryProjectNode(@RequestParam String projectId) {
        return ApiResponse.<List<ProjectNodeVo>>builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .data(iProject2Service.queryProjectNode(projectId))
                .build();
    }

    @ApiOperation(value = "24.1.4 启用/禁用流程", notes = "20000:成功；31241403：编辑项目流程失败")
    @ApiOperationSort(value = 4)
    @ThrowsAdvice(code = 31241403, print = "编辑流程设置异常")
    @Log(module = "报名流程管理", business = "编辑流程设置")
    @RequestMapping(value = "/activeNode", method = RequestMethod.POST)
    public ApiResponse<Integer> activeNode(@RequestBody ProjectNodeDTO dto) {
        return ApiResponse.<Integer>builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .data(iProject2Service.activeNode(dto))
                .build();
    }

    @ApiOperation(value = "24.1.5 流程缴费设置", notes = "20000:成功；31241501：流程缴费设置失败")
    @ApiOperationSort(value = 5)
    @ThrowsAdvice(code = 31241501, print = "流程缴费设置异常")
    @Log(module = "报名流程管理", business = "流程缴费设置")
    @RequestMapping(value = "/setProjectPay", method = RequestMethod.POST)
    public ApiResponse<Integer> setProjectPay(@RequestBody Project project) {
        return ApiResponse.<Integer>builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .data(iProject2Service.setProjectPay(project))
                .build();
    }

    @ApiOperation(value = "24.1.6 流程打印准考证设置", notes = "20000:成功；31241601：流程打印准考证设置失败")
    @ApiOperationSort(value = 6)
    @ThrowsAdvice(code = 31241601, print = "流程打印准考证设置异常")
    @Log(module = "报名流程管理", business = "流程打印准考证设置")
    @RequestMapping(value = "/setProjectAdmissionTime", method = RequestMethod.POST)
    public ApiResponse<Integer> setProjectAdmissionTime(@RequestBody Project project) {
        return ApiResponse.<Integer>builder().code(Constant.RESPONSE_CODE_SUCCESS).data(iProject2Service.setProjectAdmissionTime(project)).build();
    }

    @ApiOperation(value = "24.1.7 报名须知设置", notes = "20000:成功；31241701：报名须知设置失败")
    @ApiOperationSort(value = 7)
    @ThrowsAdvice(code = 31241701, print = "报名须知设置异常")
    @Log(module = "报名流程管理", business = "报名须知设置")
    @RequestMapping(value = "/setNotice", method = RequestMethod.POST)
    public ApiResponse<Integer> setNotice(@RequestBody ProjectNodeDTO dto) {
        return ApiResponse.<Integer>builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .data(iProject2Service.setNotice(dto))
                .build();
    }

    @ApiOperation(value = "24.1.8 报名海报上传华为云", notes = "20000:成功")
    @ApiOperationSort(value = 8)
    @RequestMapping(value = "/updatePosterUrl", method = RequestMethod.GET)
    public void updatePosterUrl() {
        iProject2Service.updatePosterUrl();
    }

    @ApiOperation(value = "24.1.9 查询考生须知", notes = "20000:成功", response = ProjectNode.class)
    @ApiOperationSort(value = 9)
    @ThrowsAdvice(code = 31050000, print = "查询考生须知异常")
    @RequestMapping(value = "/getProjectNotice", method = RequestMethod.GET)
    public ApiResponse<ProjectNode> getProjectNotice(@RequestParam String projectId) {
        return ApiResponse.<ProjectNode>builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .data(iProject2Service.getProjectNotice(projectId))
                .build();
    }

    @ApiOperation(value = "24.2.1 添加报名项目基础数据",
            notes = "20000:成功；13010101：添加项目数据失败,13010107:开始时间必须小于结束时间;",
            response = String.class)
    @ApiOperationSort(value = 10)
    @ThrowsAdvice(code = 31242101, print = "添加报名项目基础数据异常")
    @Log(module = "报名流程", business = "添加报名项目基础数据")
    @PutMapping(value = "/saveBaseProject")
    public ApiResponse<Object> saveBaseProject(@Validated(Insert.class) @RequestBody ProjectNodeBaseProjectParam param, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            return ApiResponse.<Object>builder()
                    .code(31242102)
                    .data(bindingResult.getAllErrors())
                    .build();
        }
        String projectId = null;
        if (StrUtil.isBlank(param.getId())) {
            // 赋值机构ID
            Optional.ofNullable(this.getOfficeId())
                    .ifPresent(param::setOfficeId);
            Optional.ofNullable(this.getDeptId())
                    .ifPresent(param::setDeptId);
            projectId = iProject2Service.saveBaseProject(param);
        } else {
            projectId = iProject2Service.editBaseProject(param);
        }
        return ApiResponse.builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .data(projectId)
                .build();
    }

    @ApiOperation(value = "24.2.3 保存报名流程节点数据", notes = "20000:成功；31242301：保存报名流程节点数据失败,31242302:报名流程节点数据不完整;", response = String.class)
    @ApiOperationSort(value = 12)
    @ThrowsAdvice(code = 31242301, print = "保存报名流程节点数据异常")
    @Log(module = "报名流程", business = "保存报名流程节点数据")
    @PutMapping(value = "/saveProjectNode")
    public ApiResponse<Object> saveProjectNode(@Validated(Insert.class) @RequestBody ProjectNodeParam param, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            return ApiResponse.<Object>builder()
                    .code(31242302)
                    .data(bindingResult.getAllErrors())
                    .build();
        }
        return ApiResponse.builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .data(iProject2Service.saveProjectNode(param))
                .build();
    }

    @ApiOperation(value = "24.2.5 保存报名采集数据",
            notes = "20000:成功；31242501：保存报名采集数据失败,31242502:报名采集数据不完整;",
            response = String.class)
    @ApiOperationSort(value = 14)
    @ThrowsAdvice(code = 31242501, print = "保存报名采集数据异常")
    @Log(module = "报名流程", business = "保存报名采集数据")
    @PutMapping(value = "/saveProjectCollect")
    public ApiResponse<Object> saveProjectCollect(@Validated(Insert.class) @RequestBody ProjectNodeCollectInfoParam param, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            return ApiResponse.<Object>builder()
                    .code(31242502)
                    .data(bindingResult.getAllErrors())
                    .build();
        }
        return ApiResponse.builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .data(iProject2Service.saveProjectCollect(param))
                .build();
    }

    @ApiOperation(value = "24.2.7 新增报名科目数据", notes = "20000:成功；31010133：新增项目科目数据失败")
    @ApiOperationSort(value = 16)
    @Log(module = "报名流程", business = "保存报名科目")
    @ThrowsAdvice(code = 31242701, print = "新增报名科目数据异常")
    @PutMapping(value = "/saveProjectSubject")
    public ApiResponse<Object> saveProjectSubject(@Validated(Insert.class) @RequestBody ProjectNodeSubjectParam param, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            return ApiResponse.<Object>builder()
                    .code(31242702)
                    .data(bindingResult.getAllErrors())
                    .build();
        }
        return ApiResponse.builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .data(iProject2Service.saveProjectSubject(param))
                .build();
    }

    @ApiOperation(value = "24.2.8 临时报名转正", notes = "20000:成功；31242801：报名数据保存失败")
    @ApiOperationSort(value = 17)
    @Log(module = "报名流程", business = "报名数据保存")
    @ThrowsAdvice(code = 31242801, print = "临时报名转正异常")
    @GetMapping(value = "/projectTempToMain")
    public ApiResponse<Object> projectTempToMain(@ApiParam("报名ID") @RequestParam String projectId) {
        return ApiResponse.builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .data(iProject2Service.projectTempToMain(projectId))
                .build();
    }
}
